package utilities;

import java.util.Vector;

public class Versioner<T extends Versionable<T>> {
	
	
	private static int vola = 1;
	private static int stable = 0; 
	
	private Vector<T> variable;
	
	public Versioner(T o){
		variable = new Vector<T>(Constantes.VERSIONABLE_SIZE);
		for(int i = 0; i < Constantes.VERSIONABLE_SIZE; i++){
			variable.add(o.clone());
		}
	}
	
	public T getVolatile(){
		return variable.get(vola); 
	}
	
	public T getStable(){
		return variable.get(stable);
	}
	
	public T getHistoric(int tickBack){
		if(tickBack >= variable.size())
			throw new RuntimeException("Out of versioner size: "+tickBack);
		
		int index = vola - tickBack;
		if(index < 0)
			index = variable.size()+index;
		
		return variable.get(index);
	}
	
	public static void swap(){
		stable = (stable + 1) % Constantes.VERSIONABLE_SIZE;
		vola = (vola+1) % Constantes.VERSIONABLE_SIZE;
	}
	
}
